<script setup>
import VueNextAddressPicker from "vue-next-address-picker"
import { ref, watchEffect } from "vue"
import { userStore } from '../store/index'
import { fetchApi } from '../apis/fetchapi'
import { getCurrentInstance } from 'vue'
const { proxy } = getCurrentInstance();
import GoBack from '../components/GoBack.vue'
import router from '../router';
const telreg = /^[1][3-9][0-9]{9}$/;
const userSData = userStore();
const userInfo = ref({ ...userSData.userInfo })
watchEffect(() => {
  const { connectName, connectAddress } = userInfo.value;
  if (connectName.length > 10) {
    userInfo.value.connectName = connectName.slice(0, 10)
  }
  if (connectAddress.length > 50) {
    userInfo.value.detailAddress = connectAddress.slice(0, 50)
  }
})
const showPicker = ref(false);
const choiceAddress = data => {
  showPicker.value = false;
  userInfo.value.province = data.province;
  userInfo.value.city = data.city;
  userInfo.value.district = data.area;
}
function showPickerFun() {
  showPicker.value = !showPicker.value;
}


function confirmAddress() {
  if (userInfo.value.shopName === '') {
    proxy.$toast({
      value: '请输入店铺名称',
      type: 'fail'
    })
    return false;
  }
  if (userInfo.value.connectName === '') {
    proxy.$toast({
      value: '请输入联系人',
      type: 'fail'
    })
    return false;
  }
  if ((!telreg.test(userInfo.value.connectPhone)) || userInfo.value.connectPhone === '') {
    proxy.$toast({
      value: '请输入正确的联系号码！',
      type: 'fail'
    })
    return false;
  }
  if (userInfo.value.province === '') {
    proxy.$toast({
      value: '请选择所在地区！',
      type: 'fail'
    })
    return false;
  }

  if (userInfo.value.connectAddress === '') {
    proxy.$toast({
      value: '请填写详细地址！',
      type: 'fail'
    })
    return false;
  }

  const url = '/update-info';
  fetchApi(url, userInfo.value).then(res => {
    if (res.code === 200) {
      proxy.$toast({
        value: "更新地址成功"
      })
      setTimeout(() => {
        router.go(-1);
      }, 1000)
    } else {
      proxy.$toast({
        value: res.msg,
      })
    }
  })

}

</script>
<template>
  <div class="edit-address">
    <GoBack title="我的信息"></GoBack>
    <div class="edit-con">
      <div class="edit-li">
        <div class="span"><i>*</i>店铺名称</div>
        <div class="r"><input type="text" placeholder="请输入店铺名称" v-model="userInfo.shopName"></div>
      </div>
      <div class="edit-li">
        <div class="span">所属体系</div>
        <div class="r"><input type="text" placeholder="所属体系" v-model="userInfo.schemeType" readonly></div>
      </div>
      <div class="edit-li">
        <div class="span"><i>*</i>联系人</div>
        <div class="r"><input type="text" placeholder="请输入联系人" v-model="userInfo.connectName"></div>
      </div>
      <div class="edit-li">
        <div class="span"><i>*</i>联系电话</div>
        <div class="r"><input type="text" placeholder="请输入联系电话" v-model="userInfo.connectPhone"></div>
      </div>
      <div class="edit-li">
        <div class="span"><i>*</i>所在地区</div>
        <div @click="showPickerFun()" class="choose-area" :class="{ check: userInfo.province }">
          {{ userInfo.province ? userInfo.province + " " + userInfo.city + " " + userInfo.district :
          "请选择地址"
          }}
        </div>
        <div class="choose-con" id="choose-style">
          <vue-next-address-picker :visible="showPicker" @choice="choiceAddress" :pickerLevel="3" height="600px">
          </vue-next-address-picker>
        </div>
      </div>
      <div class="edit-li">
        <div class="span-l"><i>*</i>详细地址</div>
        <div class="l-r">
          <textarea type="text" placeholder="请输入详细地址" v-model="userInfo.connectAddress"></textarea>
        </div>
      </div>
      <div class="warning">
        注：须填写打<b>*</b>号的内容才能保存该模块 <br>
        务必核实好联系人及联系地址以便及时收取活动物料
      </div>
      <div class="confirm" @click="confirmAddress">确认保存</div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.edit-address {
  width: 100%;
  height: 100vh;

  .edit-con {
    width: 100%;
    height: calc(100vh - 75px - 54px);
    background: rgb(243, 243, 243);
    margin-top: 10px;
    position: relative;
    overflow: auto;
    padding-bottom: 50px;
  }

  .edit-li {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    width: 100%;
    padding: 10px 20px;
    background-color: #ffffff;
    box-sizing: border-box;
    flex-wrap: wrap;
    position: relative;
    border-bottom: 1PX solid rgb(187, 187, 187);

    .span {
      height: 30px;
      line-height: 30px;
      font-size: 14px;
      color: rgb(16, 16, 16);

      i {
        padding-right: 4px;
        color: red;
      }
    }

    input {
      width: 200px;
      text-align: right;
      height: 30px;
      line-height: 30px;
      color: rgb(16, 16, 16);
      font-size: 14px;
    }

    .span-l {
      height: 30px;
      line-height: 30px;
      font-size: 14px;
      color: rgb(16, 16, 16);

      i {
        padding-right: 4px;
        color: red;
      }
    }

    .l-r {
      width: 250px;
      position: relative;

      textarea {
        width: 100%;
        display: block;
        height: 110px;
        color: rgb(16, 16, 16);
        font-size: 14px;
        text-align: right;
      }

    }



  }

  .warning {
    width: 340px;
    text-align: left;
    line-height: 20px;
    font-size: 14px;
    margin: 20px auto 40px;
  }

  .confirm {
    width: 348px;
    height: 46px;
    font-size: 14px;
    box-sizing: border-box;
    border: 1PX solid rgb(187, 187, 187);
    text-align: center;
    background: #ffffff;
    font-size: 16px;
    color: rgb(97, 97, 97);
    margin: 0 auto;
    line-height: 44px;
    cursor: pointer;
    border-radius: 4px;
  }

  .choose-area {
    width: 200px;
    text-align: right;
    height: 30px;
    line-height: 30px;
    color: #b9b9b9;
    font-size: 14px;

    &.check {
      color: #4f4f4f;
    }
  }

  .choose-con {
    position: absolute;
    top: 124px;
    left: 0;
    width: 100%;
    background: #333;
    z-index: 2;
  }

}
</style>
